package com.mlizhi.widgets.cursor4water;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.mlizhi.kingdom.R;

public class WaterRangeView extends RelativeLayout {

	private float minScale;
	private float normalMinScale;
	private float normalMaxScale;
	private float maxScale;
	private boolean showScale;
	
	private float bgViewWidth = 0f;
	private float bgTextSize = 30.0f;
	private int dryColor, normalColor,moistColor,textColor,cursorTextColor;
	private float mProgress;
	private CursorView cursorView = null;
	private RangeView rangeView =null;
	
	private final int DEFAULT_DRY_COLOR = Color.RED;
	private final int DEFAULT_NORMAL_COLOR = Color.BLUE;
	private final int DEFAULT_MOIST_COLOR = Color.GREEN;
	private final int DEFAULT_TEXT_COLOR = Color.parseColor("#3A3A3A");
	private final int DEFAULT_CURSOR_TEXT_COLOR = Color.GRAY;
	
	private final float DEFAULT_BG_VIEW_WIDTH = 500;
	private final int DEFAULT_PROGRESS = 80;
	private final int DEFAULT_TEXT_SIZE=18;
	
	public WaterRangeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
	}
	

	public WaterRangeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		final TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.WaterRangeView);
	        if(attributes != null){
	        	try {
	        		dryColor = attributes.getColor(R.styleable.WaterRangeView_dry_color, DEFAULT_DRY_COLOR);
	        		normalColor = attributes.getColor(R.styleable.WaterRangeView_normal_color, DEFAULT_NORMAL_COLOR);
	        		moistColor = attributes.getColor(R.styleable.WaterRangeView_moist_color, DEFAULT_MOIST_COLOR);
	        		textColor = attributes.getColor(R.styleable.WaterRangeView_text_color, DEFAULT_TEXT_COLOR);
	        		cursorTextColor = attributes.getColor(R.styleable.WaterRangeView_cursor_text_color, DEFAULT_CURSOR_TEXT_COLOR);
	        		mProgress = attributes.getFloat(R.styleable.WaterRangeView_range_progress, DEFAULT_PROGRESS);
	        		bgViewWidth = attributes.getDimension(R.styleable.WaterRangeView_bg_view_width, DEFAULT_BG_VIEW_WIDTH);
	        		bgTextSize = attributes.getDimension(R.styleable.WaterRangeView_text_size, DEFAULT_TEXT_SIZE);
	        	}finally {
	        		attributes.recycle();
	        	}
	        }
	        
		initView(context);
	}

	public WaterRangeView(Context context) {
		super(context);
	}


	/**
	 * 初始化组件
	 * @param context
	 */
	private void initView(Context context) {
		//游动标尺
		RelativeLayout.LayoutParams params4cursor = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		params4cursor.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
		params4cursor.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//		params4cursor.topMargin = 5;//距离上边距为5
		cursorView = new CursorView(context);
		cursorView.setBgTextSize(400);
		cursorView.setCursorTextColor(cursorTextColor);
		cursorView.setBgViewWidth(bgViewWidth);
		
		addView(cursorView,params4cursor);
		int height = cursorView.getCursorHeight()+38;
		//范围区间
		RelativeLayout.LayoutParams params4range = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		params4range.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
		params4range.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		params4range.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		params4range.leftMargin = 0;
		params4range.rightMargin = 0;
		params4range.topMargin = height;
		rangeView = new RangeView(context);
		rangeView.setBgTextSize(bgTextSize);
		rangeView.setBgViewWidth(bgViewWidth);
		rangeView.setDryColor(dryColor);
		rangeView.setNormalColor(normalColor);
		rangeView.setMoistColor(moistColor);
		rangeView.setTextColor(textColor);
		rangeView.initRangeView(context);
		addView(rangeView,params4range);
	}
	
	/**
	 * 本次测试，自动滑到指定位置
	 * @param mProgress
	 * @param minScale
	 * @param maxScale
	 */
	public void setProgress(float mProgress,float minScale,float maxScale){
		cursorView.setmProgress(mProgress, minScale, maxScale);
	}

	/**
	 * 根据不同部位，设置区间范围
	 * @param minScale
	 * @param normalMinScale
	 * @param normalMaxScale
	 * @param maxScale
	 * @param showScale
	 */
	public void setMeasureWidth(float minScale,float normalMinScale,float normalMaxScale,float maxScale,boolean showScale){
		rangeView.setMeasureWidth(minScale, normalMinScale, normalMaxScale, maxScale, showScale);
	}
	
	
	
	
	public boolean isShowScale() {
		return showScale;
	}

	public void setShowScale(boolean showScale) {
		this.showScale = showScale;
	}

	public float getBgViewWidth() {
		return bgViewWidth;
	}

	public void setBgViewWidth(float bgViewWidth) {
		this.bgViewWidth = bgViewWidth;
	}

	public float getBgTextSize() {
		return bgTextSize;
	}

	public void setBgTextSize(float bgTextSize) {
		this.bgTextSize = bgTextSize;
	}

	public int getDryColor() {
		return dryColor;
	}

	public void setDryColor(int dryColor) {
		this.dryColor = dryColor;
	}

	public int getNormalColor() {
		return normalColor;
	}

	public void setNormalColor(int normalColor) {
		this.normalColor = normalColor;
	}

	public int getMoistColor() {
		return moistColor;
	}

	public void setMoistColor(int moistColor) {
		this.moistColor = moistColor;
	}

	public float getMinScale() {
		return minScale;
	}

	public void setMinScale(float minScale) {
		this.minScale = minScale;
	}

	public float getNormalMinScale() {
		return normalMinScale;
	}

	public void setNormalMinScale(float normalMinScale) {
		this.normalMinScale = normalMinScale;
	}

	public float getNormalMaxScale() {
		return normalMaxScale;
	}

	public void setNormalMaxScale(float normalMaxScale) {
		this.normalMaxScale = normalMaxScale;
	}

	public float getMaxScale() {
		return maxScale;
	}

	public void setMaxScale(float maxScale) {
		this.maxScale = maxScale;
	}
	
}
